let word_data=[];
let courseName;
// let courseName = localStorage.getItem('courseName');
// let courseId_current=localStorage.getItem("courseId");
function generate() {
    console.log(courseId);

    let that = this;
    // 读取并获得模板文件的二进制内容
    JSZipUtils.getBinaryContent("../word/content_model.docx", function(error, content) {
        // model.docx是模板。我们在导出的时候，会根据此模板来导出对应的数据
        // 抛出异常
        if (error) {
            throw error;
        }
        word_data=[];
        getworddata();
        // 创建一个PizZip实例，内容为模板的内容
        let zip = new PizZip(content);
        // 创建并加载docxtemplater实例对象
        let doc = new docxtemplater().loadZip(zip);
        // 设置模板变量的值
        doc.setData({"text":"表 课程教学内容及目标",
            "table1":word_data
        });

        try {
            // 用模板变量的值替换所有模板变量
            doc.render();
        } catch (error) {
            // 抛出异常
            let e = {
                message: error.message,
                name: error.name,
                stack: error.stack,
                properties: error.properties
            };
            console.log(JSON.stringify({ error: e }));
            throw error;
        }

        // 生成一个代表docxtemplater对象的zip文件（不是一个真实的文件，而是在内存中的表示）
        let out = doc.getZip().generate({
            type: "blob",
            mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
        });
        $.ajax({

                url:'/lv_course/SelectCourseById',
                type:'get',
                dataType: 'json',
                async:false,
                contentType:'application/json',
                data:{"courseId":courseId},
                success:function (data) {
                    courseName = data.courseName;
                }
        })

        // 将目标文件对象保存为目标类型的文件，并命名
        saveAs(out, courseName+"课程教学内容及目标.docx");
    });
}

function getworddata(){
    $.ajax({
        url:'/msg/findById_content',
        type:'get',
        dataType: 'json',
        async:false,
        contentType:'application/json',
        data:{"courseId":courseId},
        success:function (data){
            for(let i in data) {
                let courseContent_Arr=[];
                let lOut_Arr=[];
                let teacheWay_Arr=[];
                let ownTarget_Arr=[];
                let all_ownTarget_Arr=[];
                courseContent_Arr.push({"courseContent":data[i].courseContent});
                lOut_Arr.push({"lOut":data[i].lOut});
                teacheWay_Arr.push({"teacheWay":data[i].teacheWay});
                ownTarget_Arr.push({"ownTarget":data[i].ownTarget});

                let courseContent = data[i].courseContent;
                let lOut = data[i].lOut;
                let teacheWay = data[i].teacheWay;
                let ownTarget = data[i].ownTarget;
                let all_ownTarget = data[i].ownTarget.split(',');
                word_data.push({"courseContent":data[i].courseContent,"lOut":data[i].lOut,"teacheWay":data[i].teacheWay,"ownTarget":data[i].ownTarget});

            }


        }
    })
}